package sns.action;

import java.util.Date;

import org.springframework.transaction.annotation.Transactional;

import sns.bean.Mblog;
import sns.dao.MblogDao;
import sns.dao.UserDao;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class ForwardAction extends ActionSupport {
	// injection
	private MblogDao mblogDao;
	private UserDao userDao;
	// pass in
	private String forwardContent;
	private Long idtargetmblog;
	private Long idpublisher;
	// return
	private Long visitId;

	// parameter
	private Mblog mblog;

	@Transactional
	public String execute() {
		
		if (visitId == null) {
			System.out.println("No visit id.");
			return ActionSupport.INPUT;
		}
		
		System.out.println("HERE HERE HERE HERE HERE HERE HERE HERE HERE HERE");
		System.out.println(idpublisher);
		System.out.println(idtargetmblog);
		System.out.println(forwardContent);
		mblog = new Mblog();

		// 如果目标微博也是转发微博，那么原始微博应该是
		if (mblogDao.getMblogById(idtargetmblog).getOriginalMblog() != null) {
			mblog.setOriginalMblog(mblogDao.getMblogById(idtargetmblog)
					.getOriginalMblog());
		} else {
			mblog.setOriginalMblog(mblogDao.getMblogById(idtargetmblog));
		}

		if (forwardContent.equals("")) {
			forwardContent = "转发微博" + forwardContent;
		}

		if (forwardContent.length() > 140) {
			forwardContent = forwardContent.substring(0, 140);
		}

		mblog.setPublisher(userDao.findUserById(idpublisher));
		mblog.setMblogTxt(forwardContent);
		mblog.setMblogTime(new Date());
		mblogDao.save(mblog);

		return Action.SUCCESS;
	}
	
	public void setForwardContent(String content) {
		this.forwardContent = content;
	}

	public void setIdtargetmblog(Long idtargetmblog) {
		this.idtargetmblog = idtargetmblog;
	}

	public void setIdpublisher(Long idpublisher) {
		this.idpublisher = idpublisher;
	}

	public void setMblogDao(MblogDao mblogDao) {
		this.mblogDao = mblogDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void setVisitId(Long visitId) {
		this.visitId = visitId;
	}

	public Long getVisitId() {
		return visitId;
	}
}
